package vip.ola.service.impl.code;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import vip.ola.core.common.Exception.ServiceException;
import vip.ola.core.entity.code.CodeAccountHistory;
import vip.ola.core.entity.code.CodeUser;
import vip.ola.core.service.code.CodeAccountHistoryService;
import vip.ola.core.service.code.CodeUserService;
import vip.ola.service.dao.mapper.code.CodeAccountHistoryMapper;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

import static java.math.BigDecimal.ROUND_HALF_UP;
import static vip.ola.core.common.constant.CodeConstant.*;
import static vip.ola.core.common.constant.MchConstant.BIZ_ITEM_CAN_SETT_MONEY;
import static vip.ola.core.common.constant.RetEnum.CODE_ORDER_SAVE_HISTORY_ERROR;

/**
 * <p>
 * 资金账户流水表 服务实现类
 * </p>
 *
 * @author faker
 * @since 2020-03-25
 */
@Service(interfaceClass = CodeAccountHistoryService.class, version = "1.0.0")
public class CodeAccountHistoryServiceImpl extends ServiceImpl<CodeAccountHistoryMapper, CodeAccountHistory> implements CodeAccountHistoryService {

    @Autowired
    private CodeUserService codeUserService;

    @Override
    public IPage<CodeAccountHistory> list(int currentPage, int pageSize, CodeAccountHistory history) {
        Page page = new Page(currentPage, pageSize);
        return baseMapper.list(page, history);
    }

    @Override
    public Map countAll(CodeAccountHistory history) {
        return baseMapper.countAll(history);
    }

    @Override
    public Map countAdd(CodeAccountHistory history) {
        return baseMapper.countAdd(history);
    }

    @Override
    public Map countSubtract(CodeAccountHistory history) {
        return baseMapper.countSubtract(history);
    }

    public Long saveCollHistory(Long userId, String orderNo, BigDecimal orderAmount,
                                BigDecimal userRate, BigDecimal userProfit, BigDecimal agentRate, BigDecimal agentProfit,
                                BigDecimal codeBusRate, BigDecimal codeBusProfit) {
        if (userProfit.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        CodeUser user = codeUserService.getById(userId);
        CodeAccountHistory history = new CodeAccountHistory();

        history.setCodeBusRate(codeBusRate);
        history.setCodeBusProfit(codeBusProfit);
        history.setAgentRate(agentRate);
        history.setAgentProfit(agentProfit);

        history.setUserId(user.getId());
        history.setUsername(user.getUsername());
        history.setAgentId(user.getAgentId());
        history.setCodeBusId(user.getCodeBusId());
        history.setOrderNo(orderNo);
        history.setOrderAmount(orderAmount);
        BigDecimal changeAmount =  orderAmount.subtract(userProfit);
        history.setBeforeAmount(user.getCanSettAmount().subtract(changeAmount).setScale(2, ROUND_HALF_UP));
        history.setAmount(changeAmount);
        history.setAfterAmount(user.getCanSettAmount());
        history.setRate(userRate);
        history.setUserProfit(userProfit);
        history.setBizItem(BIZ_ITEM_CAN_SETT_MONEY);
        history.setBizType(TYPE_COLL);
        history.setFundDirection(FUND_ADD);
        history.setCreateTime(LocalDateTime.now());
        boolean isOk = SqlHelper.retBool(baseMapper.insert(history));
        if (!isOk) {
            throw new ServiceException(CODE_ORDER_SAVE_HISTORY_ERROR);
        }
        return history.getId();
    }

}
